package middle;

public class Solution_43 {
    public String multiply(String num1, String num2) {
        if (num2.length() > num1.length()) {
            return multiply(num2, num1);
        }
        char[] array1 = num1.toCharArray();
        char[] array2 = num2.toCharArray();
        StringBuilder origin = new StringBuilder().append('0');
        for (int i = 0; i < array2.length; i++) {
            StringBuilder builder = multiplySingle(array1, array2[array2.length - i - 1] - '0', i);
            if (builder.length() == 1 && builder.charAt(0) == '0') {
                continue;
            }
            origin = add(origin, builder);
        }
        return origin.toString();
    }

    private StringBuilder multiplySingle(char[] chars, int num, int offset) {
        StringBuilder builder = new StringBuilder();
        if (num == 0) {
            return new StringBuilder().append(0);
        }
        int pre = 0;
        for (int i = chars.length - 1; i >= 0; i--) {
            int n = chars[i] - '0';
            int multi = n * num + pre;
            if (multi > 9) {
                pre = multi / 10;
                multi = multi % 10;
            } else {
                pre = 0;
            }
            builder.append(multi);
        }
        if (pre > 0) {
            builder.append(pre);
        }
        if (builder.length() == 1 && builder.charAt(0) == '0') {
            return builder;
        }
        StringBuilder reverse = builder.reverse();
        for (int i = 0; i < offset; i++) {
            reverse.append('0');
        }
        return reverse;
    }

    private StringBuilder add(StringBuilder add1, StringBuilder add2) {
        if (add2.length() > add1.length()) {
            return add(add2, add1);
        }
        int pre = 0;
        add1 = add1.reverse();
        add2 = add2.reverse();
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < add1.length(); i++) {
            int num1 = add1.charAt(i) - '0', num2;
            if (i >= add2.length()) {
                num2 = 0;
            } else {
                num2 = add2.charAt(i) - '0';
            }

            int sum = num1 + num2 + pre;
            if (sum > 9) {
                pre = sum / 10;
                sum = sum % 10;
            } else {
                pre = 0;
            }
            builder.append(sum);
        }
        if (pre > 0) {
            builder.append(pre);
        }
        return builder.reverse();
    }

    public static void main(String[] args) {
        Solution_43 model = new Solution_43();
        System.out.println(model.multiply("0", "3"));
        System.out.println(model.multiply("1", "0"));
        System.out.println(model.multiply("2", "3"));
        System.out.println(model.multiply("123", "456"));
//        System.out.println(model.multiplySingle(new char[]{'1', '2', '3'}, 4, 0));
//        System.out.println(model.multiplySingle(new char[]{'1', '2', '3'}, 5, 1));
//        System.out.println(model.multiplySingle(new char[]{'1', '2', '3'}, 6, 2));
//        System.out.println(model.add(new StringBuilder("0"), new StringBuilder("492")));
//        System.out.println(model.add(new StringBuilder("492"), new StringBuilder("6150")));
//        System.out.println(model.add(new StringBuilder("6642"), new StringBuilder("73800")));
    }
}
